import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import login_required
from models import *
from datetime import datetime
import logging

class ProjectHandler(webapp.RequestHandler):

    def post(self):
        name = self.request.get('name', False)
        secret_key = self.request.get('secret_key', False)
        cia_name = self.request.get('cia_name', False)
        if not users.get_current_user():
            self.response.out.write('not logged in')
            return
        if name is False or secret_key is False:
            self.response.out.write('did not fill out required fields')
            return
        user = users.get_current_user()
        time = datetime.now()
        project = Project(
            name=name,
            owner=user,
            secret_key=secret_key,
            last_update=time,
        )
        project.put()
        self.response.out.write('project created. now configure web hooks on your project to POST to http://cia-notifier.appspot.com')

def main():
    application = webapp.WSGIApplication([('/new_project.html', ProjectHandler)],
                               debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
